package club.samubird.simpledatahandlekit.utils;

import java.util.List;

/**
 * @author sam
 * @date 2019/10/9 18:09
 * @description 注意
 */
public class SimpleListLoop<T> {

	private List<T> loopObj;

	private Integer batchSize;

	private Integer currentIndex;

	private Integer nextIndex;

	private Integer maxSize;

	public SimpleListLoop(List<T> loopObj) {
		this.loopObj = loopObj;
		this.batchSize = 1000;
		this.maxSize = loopObj.size();
		this.currentIndex = 0;
		this.nextIndex = this.currentIndex + this.batchSize > this.maxSize ? this.maxSize : this.currentIndex + this.batchSize;
	}

	public SimpleListLoop(List<T> loopObj, Integer batchSize) {
		this.loopObj = loopObj;
		this.batchSize = batchSize;
		this.maxSize = loopObj.size();
		this.currentIndex = 0;
		this.nextIndex = this.currentIndex + this.batchSize > this.maxSize ? this.maxSize : this.currentIndex + this.batchSize;
	}

	public boolean hasNext() {
		return this.nextIndex > this.currentIndex;
	}

	public List<T> next() {
		List<T> ts = loopObj.subList(this.currentIndex, this.nextIndex);
		this.currentIndex += this.batchSize;
		this.nextIndex = this.currentIndex + this.batchSize > this.maxSize ? this.maxSize : this.currentIndex + this.batchSize;
		return ts;
	}

	public Integer getTotalBatchNum() {
		return this.maxSize % this.batchSize == 0 ? this.maxSize / this.batchSize : (this.maxSize / this.batchSize + 1);
	}

	public Integer getCurrentBatchIndex() {
		return this.currentIndex % this.batchSize == 0 ? this.currentIndex / this.batchSize : (this.currentIndex / this.batchSize + 1);
	}

}
